package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cache.MemoryCache;
import com.example.cache.MissionCache;
import com.example.conf.RedisJsonConf;
import com.example.pojo.*;
import com.example.service.ROVService;
import com.example.util.ContextUtil;
import com.example.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class ROVServiceImpl implements ROVService {
    @Value("${fastgpt.key.monitor}")
    private String FastGptKeyMonitor;//状态检查key
    @Value("${fastgpt.key.fault}")
    private String FastGptKeyFault;
    @Value("${fastgpt.key.summary}")
    private String FastGptKeySummary;
    @Value("${fastgpt.key.order}")
    private String FastGptKeyOrder;
    @Autowired
    private AccessToFastGpt accessToFastGpt;
    @Autowired
    private MissionCache missionCache;
    @Autowired
    private MemoryCache memoryCache;
    @Autowired
    private ContextUtil contextUtil;
    @Value("${fastgpt.key.rec}")
    private String FastGptKeyRec;
    /**
    * 获取ROV状态监控
    */
    @Override
    public FastgptVO getInfo(String Content) {
        return accessToFastGpt.getAccess(Content, FastGptKeyMonitor, false, false);
    }

    /**
     * ROV故障诊断
     */
    @Override
    public FastgptVO getFault(String Content) {
        return accessToFastGpt.getAccess(Content, FastGptKeyFault, false, false);
    }

    /**
     * 获取ROV工作报告
     */
    @Override
    public String getSummary(Mission mission) {
        String content = contextUtil.getLogs(mission);
        FastgptVO fastgptVO = accessToFastGpt.getAccess(content, FastGptKeySummary, false, false);
        JSONObject rsJson = JSON.parseObject(JSONObject.toJSONString(fastgptVO));
        return rsJson.getString("content");
    }

    @Override
    public boolean createMission(Mission mission) {
        missionCache.put(mission.getId(), mission);
        return true;
    }

    @Override
    public List<Mission> queryAllMission() {
        return missionCache.queryAll();
    }

    @Override
    public MissionVO generateLocation(Mission mission) {
        //生成位置
        String content = "经纬度位置: "+mission.getLongitude() + "," + mission.getLatitude()+ "," + mission.getRequirement();
        FastgptVO fastgptVO = accessToFastGpt.getAccess(content, FastGptKeyOrder, false, false);
        JSONObject rsJson = JSON.parseObject(JSONObject.toJSONString(fastgptVO));
        JSONArray arr = rsJson.getJSONArray("content");
        Location l1 = new Location(arr.getJSONObject(0).getString("longitude"),arr.getJSONObject(0).getString("latitude"));
        Location l2 = new Location(arr.getJSONObject(1).getString("longitude"),arr.getJSONObject(1).getString("latitude"));
        Location l3 = new Location(arr.getJSONObject(2).getString("longitude"),arr.getJSONObject(2).getString("latitude"));
        Location l4 = new Location(arr.getJSONObject(3).getString("longitude"),arr.getJSONObject(3).getString("latitude"));
        List<Location> locations = new ArrayList<>();
        locations.add(l1);
        locations.add(l2);
        locations.add(l3);
        locations.add(l4);
        MissionVO missionVO = new MissionVO();
        missionVO.setLocations(locations);
        //记录上下文
        contextUtil.memoryLocations(mission, missionVO);
        return missionVO;
    }

    public LogVO getLogs(Mission mission){
        //调用大模型接口能力
        StringBuilder sb = new StringBuilder();
        sb.append(contextUtil.getLogs(mission));
        String content = "当前ROV经纬度位置: 经度："+mission.getLongitude()+",纬度："+mission.getLatitude();
        sb.append(content);
        FastgptVO fastgptVO = getInfo(sb.toString());
        JSONObject rsJson = JSON.parseObject(JSONObject.toJSONString(fastgptVO));
        JSONObject response = JSON.parseObject(rsJson.getString("content"));
        String info = response.getString("info");
        String fatal = response.getString("fatal");
        LogVO logVO = new LogVO();
        logVO.setInfo(info);
        logVO.setFatal(fatal);
        //记录上下文
        contextUtil.memoryLogs(mission, logVO);
        return logVO;
    }

    @Override
    public String getMemo(String id) {
        return memoryCache.debug(id);
    }

    @Override
    public JSONArray recommend(String Content) {
        FastgptVO fastgptVO = accessToFastGpt.getAccess(Content, FastGptKeyRec, false, false);
        JSONObject rsJson = JSON.parseObject(JSONObject.toJSONString(fastgptVO));
        JSONObject json = JSON.parseObject(rsJson.getString("content"));
        return JSON.parseArray(json.getString("questions"));
    }

    @Autowired
    RedisUtil redisUtil;

    @Override
    public Map<String, String> GetROVData(){
        String key = "0x31000001";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/ROV接收协议.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetZYCData(){
        String key = "0x31000008";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/作业舱.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetBZJKData(){
        String key = "0x31000006";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/保障监控装置接口协议.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetGUIDEData(){
        String key = "0x32000001";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/导航.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetDLBTData(){
        String key = "0x31000004";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/打捞本体和绞车.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetJXBData(){
        String key = "0x31000002";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/机械臂接收协议.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetQXQHData(){
        String key = "0x31000009";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/权限切换.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetMKHDZData(){
        String key = "0x31000003";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/模块化搭载.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Map<String, String> GetPLData(){
        String key = "0x31000005";
        Map<Object, Object> result = redisUtil.hmget(key);
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        Map<String, String> validValues;
        try {
            validValues = redisJsonConf.processData_Get(result, "src/main/java/com/example/conf/JSON/铺缆接收协议.json");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return validValues;
    }

    @Override
    public Boolean SetROVData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/ROV接收协议.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000001", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetZYCData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/作业舱.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000008", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetBZJKData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/保障监控装置接口协议.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000006", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetGUIDEData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/导航.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x32000001", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetDLBTData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/打捞本体和绞车.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000004", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetJXBData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/机械臂接收协议.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000002", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetQXQHData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/权限切换.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000009", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetMKHData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/模块化搭载.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000003", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public Boolean SetPLData(Map<String, String> data){
        Boolean result = false;
        RedisJsonConf redisJsonConf = new RedisJsonConf();
        try {
            Map<String, Object> RedisData = redisJsonConf.processData_Set(data, "src/main/java/com/example/conf/JSON/铺缆接收协议.json");
            if(RedisData != null){
                if((boolean) RedisData.get("isContainRedisKey") && (boolean) RedisData.get("isContainRedisValue")) {
                    RedisData.remove("isContainRedisKey");
                    RedisData.remove("isContainRedisValue");
                    result = redisUtil.hmset("0x31000005", RedisData);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
